package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.WorkerBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.WorkerDao;
import com.jackiechancinema.ssm.dto.DepartmentDto;
import com.jackiechancinema.ssm.dto.WorkerDto;
import com.jackiechancinema.ssm.entity.Department;
import com.jackiechancinema.ssm.entity.Worker;
import com.jackiechancinema.ssm.listener.DepartmentReadListener;
import com.jackiechancinema.ssm.listener.WorkerReadListener;
import com.jackiechancinema.ssm.service.Inte.WorkerService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Repository
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerDao dao;
    @Autowired
    private WorkerReadListener listener;


    @Override
    public R insertWork(Worker worker) {
        int i = dao.insertWork(worker);
        if (i>0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R updateWork(Worker worker) {
//        System.out.println(worker.getWork_name());
        int i = dao.updateWork(worker);
        if (i>0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R deleteWork(int work_id) {
        int i = dao.deleteWork(work_id);
        if (i>0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public PageVo<Worker> page(int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
//        System.out.println(dao.selectAll());
        PageInfo<Worker> pageInfo=new PageInfo<Worker>(dao.queryAll());
        PageVo<Worker> pageVo = new PageVo<Worker>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        return pageVo;
    }

    @Override
    public PageVo<Worker> page2(WorkerBo bo) {
        Worker worker = new Worker();
        if (bo!=null) {
            if (StringUtils.hasLength(bo.getWork_name())){
//                System.out.println(bo.getWork_name());
                worker.setWork_name(bo.getWork_name());
            }
            if (StringUtils.hasLength(bo.getWork_sex())){
                System.out.println(bo.getWork_sex());
                worker.setWork_sex(bo.getWork_sex());
            }
            if (StringUtils.hasLength(bo.getWork_birthday())){
                worker.setWork_birthday(bo.getWork_birthday());
            }
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            PageInfo<Worker> pageInfo = new PageInfo<Worker>(dao.queryBy(worker));
            return new PageVo<Worker>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Worker>(SystemContant.PAGE_R,"OK",0,null);
    }

    /**
     * 批量新增
     * @param file
     * @return
     */
    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
//              System.out.println(file.getName());
                EasyExcel.read(file.getInputStream(), WorkerDto.class,listener).sheet().doRead();
                System.out.println("entter2");
                return R.ok(null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    /**
     * 导出
     * @param bo
     * @param response
     */
    @Override
    public void exportExcel(WorkerBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
                String fn=System.currentTimeMillis()+".xlsx";
                //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
                response.setHeader("content-disposition","attachment;filename="+fn);
        //        System.out.println(bo.getDname());
                //2.组装查询条件
                Worker worker=new Worker();
                if(StringUtils.hasLength(bo.getWork_name())){
                    worker.setWork_name(bo.getWork_name());
                }
                if(StringUtils.hasLength(bo.getWork_sex())){
                    worker.setWork_sex(bo.getWork_sex());
                }
                if(StringUtils.hasLength(bo.getWork_birthday())){
                    worker.setWork_birthday(bo.getWork_birthday());
                }

                //3.获取要下载的数据
                List<Worker> list=dao.queryBy(worker);
                try {
                    //4.生成Excel，并将数据写出到响应流中
                    EasyExcel.write(response.getOutputStream(),Worker.class).sheet().doWrite(list);

                } catch (IOException e) {
                    e.printStackTrace();
                }
    }
}
